﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using QLPKWeb.Models.FormBean.DanhMucChungModel;
using QLPKWeb.Models.LinQ.DanhMucChung;
using Webdiyer.WebControls.Mvc;
using DONGACS.Common;


namespace QLPKWeb.BussinessDelegate.DanhMucChung
{
    public class NgoaiTeMonneyDelegate
    {
        DBDanhMucChungDataContext DBDanhMucChung = new DBDanhMucChungDataContext();

        public IList<dm_NgoaiTe_TiGia> GetAlldm_NgoaiTe()
        {
            return DBDanhMucChung.dm_NgoaiTe_TiGias.ToList();
        }
        public dm_NgoaiTe_TiGia AddNewNgoaiTe(dm_NgoaiTe_TiGia dm_NgoaiTe)
        {
            DBDanhMucChung.dm_NgoaiTe_TiGias.InsertOnSubmit(dm_NgoaiTe);
            DBDanhMucChung.SubmitChanges();
            return GetNgoaiTeIdMax();
        }

        public void DeleteNgoaiTe(string dm_NgoaiTeID)
        {
            dm_NgoaiTe_TiGia dm_NgoaiTeNew =
                DBDanhMucChung.dm_NgoaiTe_TiGias.Single(p => p.Id == Convert.ToInt32(dm_NgoaiTeID));
            //dm_NgoaiTe.Id = dm_NgoaiTe.Id;

            DBDanhMucChung.dm_NgoaiTe_TiGias.DeleteOnSubmit(dm_NgoaiTeNew);
            DBDanhMucChung.SubmitChanges();
        }

        public void UpdateNgoaiTe(dm_NgoaiTe_TiGia dm_NgoaiTe)
        {
            dm_NgoaiTe_TiGia dm_NgoaiTeUpdate =
                DBDanhMucChung.dm_NgoaiTe_TiGias.Single(p => p.Id == dm_NgoaiTe.Id);
            dm_NgoaiTeUpdate.GiaTriTiGia = dm_NgoaiTe.GiaTriTiGia;
            dm_NgoaiTeUpdate.IsActive = dm_NgoaiTe.IsActive;
            DBDanhMucChung.SubmitChanges();
        }

        public PagedList<dm_NgoaiTe_TiGia> SearchNgoaiTePagedList(int? id, string searchCriteria)// id = page Index
        {
            return (PagedList<dm_NgoaiTe_TiGia>)DBDanhMucChung.dm_NgoaiTe_TiGias.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage).Where(p => p.GiaTriTiGia == searchCriteria);

        }
        public dm_NgoaiTe_TiGia GetNgoaiTeById(string id)
        {
            try
            {
                dm_NgoaiTe_TiGia item = this.DBDanhMucChung.dm_NgoaiTe_TiGias.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }
        public void Active(int id)
        {
            dm_NgoaiTe_TiGia item = this.DBDanhMucChung.dm_NgoaiTe_TiGias.Single(p => p.Id == Convert.ToInt32(id));
            item.IsActive = true;
            foreach (var dwDmnvlThayDoiMoney in this.GetNgoaiTeMonneyByIdNgoaiTe(item.IdNgoaiTe.Value).Where(p=>p.Id!=item.Id))
            {
                dwDmnvlThayDoiMoney.IsActive = false;
            }
            DBDanhMucChung.SubmitChanges();
        }
        public dm_NgoaiTe_TiGia GetNgoaiTeByTen(string Ten)
        {
            IEnumerable<dm_NgoaiTe_TiGia> list = this.DBDanhMucChung.dm_NgoaiTe_TiGias.Where(p => p.GiaTriTiGia == Ten);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public PagedList<dm_NgoaiTe_TiGia> GetNgoaiTePagedList(int? id)// id = page Index
        {
            return DBDanhMucChung.dm_NgoaiTe_TiGias.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }
        public dm_NgoaiTe_TiGia BuildNewNgoaiTe(NgoaiTeMonneyForm form)
        {
            CommonDelegate.CommonDelegate  CommonDelegate= new CommonDelegate.CommonDelegate();
            dm_NgoaiTe_TiGia newdm_NgoaiTe = new dm_NgoaiTe_TiGia();
            newdm_NgoaiTe.IdNgoaiTe = form.IdNgoaiTe;
            newdm_NgoaiTe.GiaTriTiGia = form.GiaTriTiGia;
            return newdm_NgoaiTe;
        }
        public dm_NgoaiTe_TiGia BuildEditNgoaiTe(NgoaiTeMonneyForm form)
        {
            CommonDelegate.CommonDelegate CommonDelegate = new CommonDelegate.CommonDelegate();
            dm_NgoaiTe_TiGia newdm_NgoaiTe = new dm_NgoaiTe_TiGia();
            newdm_NgoaiTe.Id = form.Id;
            newdm_NgoaiTe.IdNgoaiTe = form.IdNgoaiTe;
            newdm_NgoaiTe.GiaTriTiGia = form.GiaTriTiGia;
            return newdm_NgoaiTe;
        }
        public dm_NgoaiTe_TiGia GetNgoaiTeIdMax()
        {
            IList<dm_NgoaiTe_TiGia> list = DBDanhMucChung.dm_NgoaiTe_TiGias.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public IList<dm_NgoaiTe_TiGia> GetNgoaiTeMonneyByIdNgoaiTe(int IdNgoaiTe)
        {
            IList<dm_NgoaiTe_TiGia> list = DBDanhMucChung.dm_NgoaiTe_TiGias.Where(p=>p.IdNgoaiTe==IdNgoaiTe).ToList();
            return list;
        }

      
    }
}